<?php
/**
 * mysql数据操作类
 * 1.该类一个实例化，就可以自动连接上mysql数据库
 * 2.该类可以单独去设定要使用的连接编码
 * 3.该类可以单独去设定要使用的数据库
 * 4.可以主动关闭连接
 */
class MySQLDB
{
    public $link = null;
    private $host;
    private $port;
    private $user;
    private $pass;
    private $charset;
    private $dbname;
    function __construct($config)
    {
        $this->host=!empty($config['host'])?$config['host']:"localhost";
        $this->port=$config['port'];
        $this->user=$config['user'];
        $this->pass=$config['pass'];
        $this->charset=$config['charset'];
        $this->dbname=$config['dbname'];

        $this->link = @mysql_connect("{$this->host}:{$this->port}","{$this->user}","{$this->pass}") or die("连接失败");
        //mysql_query("set names {$this->charset}");
        $this->setCharset($this->charset);
        //mysql_query("use {$this->dbname}");
        $this->selectDB($this->dbname);
    }
    //设置编码
    function setCharset($charset)
    {
        mysql_query("set names $charset");
    }
    //选择数据库
    function selectDB($dbname)
    {
        mysql_query("use $dbname");
    }
    //关闭连接
    function closeDB()
    {
        mysql_close($this->link);
    }
    function exec($sql){
        $result = mysql_query($sql);
        if($result===false){
            //return $result;
            echo "<p>sql语句执行失败，请参考如下信息</p>";
            echo "</br>错误代号：".mysql_errno();
            echo "</br>错误信息：".mysql_error();
            echo "</br>错误语句：".$sql;
            die();
        }
        return true;
    }
    function getOneRow($sql){
        $result = mysql_query($sql);
        if($result===false){
            echo "<p>sql语句执行失败，请参考如下信息</p>";
            echo "</br>错误代号：".mysql_errno();
            echo "</br>错误信息：".mysql_error();
            echo "</br>错误语句：".$sql;
            die();
        }
        return mysql_fetch_assoc($result);
    }
    function getRows($sql){

    }
    function getOneData($sql){

    }

}